package com.googlecode.jlogalizerweb.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class FileAppendUtils {
	
	private FileAppendUtils(){};
	
	private static FileAppendUtils instance=null;
	
	public static FileAppendUtils getInstance() {
	      if(instance == null) {
	    	  synchronized(FileAppendUtils.class){
	    		  instance = new FileAppendUtils();
	    	  }
	      }
	      return instance;
	 }

	/*public static void appendToFile(Reader reader, File file) throws IOException{
	
		IOUtils.copy(reader, outputStream(file));
	}
	
	public static void appendToFile(String inputString, File file) throws IOException{
		appendToFile(new StringReader(inputString), file);
		
	}
	
	public static void appendToFile(String inputString, String filePath) throws IOException{
		appendToFile(inputString, new File(filePath));
		
	}
	
	private static OutputStream outputStream (File file) throws IOException{
		return new FileOutputStream(file, true);
	}*/
	
	
	public static void appendToFile(String inputString, String filePath) throws IOException{
		appendToFile(inputString, new File(filePath));
		
	}

	public static void appendToFile(String inputString, File file) throws IOException{
		
		System.out.println("File complete path : "+file.getAbsolutePath());
		if (!(file.exists())){
			file.createNewFile();
		}
		FileWriter writer=new FileWriter(file, true);
		PrintWriter printWriter = new PrintWriter(writer);
		printWriter.println(inputString); 
		printWriter.close(); 
		writer.close();
		
	}
	
	public static void main(String[] args) throws IOException {
		FileAppendUtils test = FileAppendUtils.getInstance();
		test.appendToFile("Helloooo from arun", "/Users/Jason/tmp/file");
		test.appendToFile("Helloooo from arun123", "/Users/Jason/tmp/file");
		
		System.out.println("done");
		
	}
}
